package ro.sergiu.finder.models.finder.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import ro.sergiu.finder.models.finder.FinderModel;

/**
 *
 * @author Toporjinschi Is the component which helps to design the search dialog.
 * @param <B> the bean which will be used for returning the selection
 */
public abstract class DbFinderModel<B> extends FinderModel {

    /**
     * Return the table name for search.<br>
     * Ex: <code>users</code> or <code>users u</code> for using the alias.<br>
     * This can contain a complex from clause for example:<br>
     * <code> users u, settings s</code> in this case {@link #hasExtraFilters()} should have the
     * join condition
     *
     * @return the table name
     */
    public abstract String getTableName();

    /**
     * Populate bean is used for populating a bean from {@link ResultSet} for returning the current
     * selected record
     *
     * @param rs result set
     * @return the bran populated
     * @throws SQLException in case of sql error
     */
    public abstract B populateBean(ResultSet rs) throws SQLException;

    /**
     * Override this method if you want to do some extra filters and not use all table records;<br>
     * Ex: {@code active = 1} this will return a selection window only with active users
     *
     * @return the extra filters
     */
    public String getExtraFilters() {
        return "";
    }

    /**
     *
     * @return Returns true if there is an extra filter.
     */
    public boolean hasExtraFilters() {
        String extraFilter = this.getExtraFilters();
        return extraFilter != null && !extraFilter.trim().isEmpty();
    }

}
